﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Windows.Markup
Imports System.Drawing
Imports System.Activities.Presentation.Metadata
Imports System.Activities.Core.Presentation

Public Class BookmarkActivity
    Inherits NativeActivity
    Implements IRegisterMetadata

#Region "プロパティ"
    <RequiredArgument()>
    Public Property BookmarkName As InArgument(Of String)

    Public Property HostedValue As OutArgument(Of String)
#End Region

    Public Sub New()
        Me.DisplayName = GetResourceStrings("ActBMDisplayName", "ブックマーク作成")
    End Sub

    Protected Overrides ReadOnly Property CanInduceIdle As Boolean
        Get
            Return True
        End Get
    End Property

    Protected Overloads Overrides Sub Execute(ByVal context As System.Activities.NativeActivityContext)
        Dim bookName = context.GetValue(Me.BookmarkName)
        If bookName.Trim = "" Then Return
        context.CreateBookmark(bookName, New BookmarkCallback(AddressOf OnResume))
    End Sub

    ''' <summary>ブックマークが呼び出された際に呼ばれるコールバック</summary>
    Private Sub OnResume(ByVal context As NativeActivityContext, ByVal bookmark As Bookmark, ByVal value As Object)
        context.SetValue(Me.HostedValue, value)
    End Sub

#Region "メタデータ"

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim metadata As New DesignerMetadata
        metadata.Register()

        Dim builder As New AttributeTableBuilder
        builder.AddCustomAttributes(GetType(BookmarkActivity),
                            New DesignerAttribute(GetType(BookmarkActivityDesigner)),
                            New ToolboxBitmapAttribute("Images\Activity\book_notebook.png")
                            )
        builder.AddCustomAttributes(GetType(BookmarkActivity), "BookmarkName",
                                    New CategoryAttribute(GetResourceStrings("ACtBMPropertyCategory", "処理の設定")),
                                    New DisplayNameAttribute(GetResourceStrings("ACtBMBookmarknameProperty", "ブックマーク名")),
                                    New DescriptionAttribute(GetResourceStrings("ACtBMBookmarknameDescription", "作成するブックマーク名を設定します")))
        builder.AddCustomAttributes(GetType(BookmarkActivity), "HostedValue",
                                    New CategoryAttribute(GetResourceStrings("ACtBMPropertyCategory", "処理の設定")),
                                    New DisplayNameAttribute(GetResourceStrings("ACtBMHostedvalueProperty", "結果を受取る変数")),
                                    New DescriptionAttribute(GetResourceStrings("ACtBMHostedvalueDescription", "ブックマークに渡された値を設定する変数を指定します")))

        MetadataStore.AddAttributeTable(builder.CreateTable)
    End Sub

#End Region

#Region "その他"

    ''' <summary>リソースより文字列の取得</summary>
    Private Shared Function GetResourceStrings(ByVal resourceName As String, ByVal defaultStrings As String)
        Dim locMes = CStr(Application.Current.TryFindResource(resourceName))
        If (locMes Is Nothing) OrElse (locMes.Trim = "") Then locMes = defaultStrings
        Return locMes
    End Function

#End Region

End Class
